package com.example.teamtribe.common;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.common.userinfo.UserInfoActivity;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.ViewHolder> {

    private List<User> users;
    private Context context;

    public UsersAdapter() {
        users = new ArrayList<>();
    }

    public void set(List<User> users) {
        users.remove(Utility.getUser());
        this.users = users;
    }

    public void add(List<User> newData) {
        users.remove(Utility.getUser());
        users.addAll(newData);
    }

    public void add(User data) {
        users.add(data);
    }

    public void clear() {
        users.clear();
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_chat, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("user", users.get(position));//0的位子是header，要减掉1
                context.startActivity(intent);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        User user = users.get(position);
        Glide.with(context).load(Utility.getUri(user.getPortrait())).into(holder.ivHead);
        holder.tvNickname.setText(user.getNickname());
        holder.tvInfo.setText(user.getGenderString());
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    static public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView ivHead;
        public TextView tvNickname;
        public TextView tvInfo;
        public Badge badge;

        public ViewHolder(View itemView) {
            super(itemView);
            ivHead = itemView.findViewById(R.id.iv_head);
            tvNickname = itemView.findViewById(R.id.tv_nickname);
            tvInfo = itemView.findViewById(R.id.tv_info);
            badge = new QBadgeView(itemView.getContext()).bindTarget(tvNickname);
            badge.setBadgeGravity(Gravity.CENTER | Gravity.END);
        }
    }
}
